<?php

class Client_Table_Objects extends Apk_Db_Table
{
	protected $_name      = 'objects';
    protected $_primary   = 'id';
    protected $_ERROR = '';
    protected $_lang;

    public function __construct($lang = 'ru')
    {
    	$this->_lang = $lang;
    	parent::__construct();
    }

    public function GetError()
    {
    	return $this->_ERROR;
    }

    public function GetDataList($params=null, $is_rand = 0)
    {
    	$sel = $this->getAdapter()->select()
    		  ->from($this->_name, array('id', 'title' => 'title_'.$this->_lang, 'desc' => 'desc_'.$this->_lang, 'path', 'file_name', 'id_cat', 'id_events', 'id_gallery', 'id_article', 'is_top'))
    		  ->where($this->_name.'.is_ghost = 0')
    		  ->where($this->_name.'.is_removed = 0')
    		  ->where($this->_name.'.is_active')
    		  ->order($this->_name.'.norder');

    	if(!is_null($params)){
    		foreach($params as $param => $val)
    		switch ($param)
    		{
    			default:
    				$sel->where($param.' = ?', $val);
    				break;
    		}
    	}

    	$a =  $this->setDecorator('simpleDecorator')->getList($sel)->toArray();


		//if($is_rand) $a = $this->array_rand($a);

		return $a;
    }

    public function GetDataListPager($page=1, $sort=null, $page_count=null, $params=null)
    {
    	$sel = $this->getAdapter()->select()
    		  ->from($this->_name, array('id', 'path', 'title' => 'title_'.$this->_lang, 'add_date', 'is_active', 'file_name'))
    		  ->where($this->_name.'.is_ghost = 0')
    		  ->where($this->_name.'.is_removed = 0');

    	if(!is_null($params)){
    		foreach($params as $param => $val)
    		switch ($param)
    		{
    			default:
    				$sel->where($param.' = ?', $val);
    				break;
    		}
    	}

		if(!is_null($sort) && strlen($sort) > 0) $sel->order($this->_name.'.'.$sort);
		else $sel->order($this->_name.'.norder');

    	return $this->setDecorator('simpleDecorator')->getPager($sel, $page, array('perPage' => $page_count));
    }

    public function GetDataListTable($page = null, $params=array(), $c = 3, $page_count = null)
    {
    	$sel = $this->getAdapter()->select()
    		  ->from($this->_name, array('id', 'title' => 'title_'.$this->_lang, 'desc' => 'desc_'.$this->_lang, 'path', 'file_name', 'id_cat', 'id_products', 'id_gallery', 'id_article'))
    		  ->where($this->_name.'.is_ghost = 0')
    		  ->where($this->_name.'.is_removed = 0')
    		  ->where($this->_name.'.is_active')
    		  ->order($this->_name.'.norder');

    	if(count($params) > 0){
    		foreach($params as $param => $val)
    		switch ($param)
    		{
    			default:

    				$sel->where($param.' = ?', $val);
    				break;
    		}
    	}


    	if($page > 0)
    	{
	    	$data = $this->setDecorator('simpleDecorator')->getPager($sel, $page, array('perPage' => $page_count));

	    	$ret = array();
	    	$r = -1;
	    	foreach($data['data'] as $k => $v)
	    	{
	    		if($k%$c == 0) $r++;
	    		$ret['data'][$r][] = $v;
	    	}
	    	$ret['pager'] = $data['pager'];
    	}
    	else
    	{
	    	$data = $this->setDecorator('simpleDecorator')->getList($sel)->toArray();

	    	$ret = array();
	    	$r = -1;
	    	foreach($data as $k => $v)
	    	{
	    		if($k%$c == 0) $r++;
	    		$ret[$r][] = $v;
	    	}
    	}

    	return $ret;
    }

    public function GetDataRow($id)
    {
    	$sel = $this->getAdapter()->select()
    				->from($this->_name, array('id', 'title' => 'title_'.$this->_lang, 'desc' => 'desc_'.$this->_lang, 'path', 'file_name', 'id_cat', 'id_products', 'id_gallery', 'id_article'))
  	    		    ->where('is_ghost = 0')
    		  		->where('is_removed = 0')
    		  		->where('is_active')
    				->where('id = ?', $id);

    	return $this->setDecorator('simpleDecorator')->getList($sel)->getRow(0)->toArray();
    }

    public function GetComments($id)
    {
    	$sel = $this->getAdapter()->select()
    		  ->from('objects_comments')
    		  ->join('accounts', 'accounts.id = objects_comments.add_user_id', array('username', 'path', 'file_name'))
    		  ->where('objects_comments.id_object = ?', $id);

    	return $this->setDecorator('simpleDecorator')->getList($sel)->toArray();
    }

    //**************************************
    // update info

	public function insert(array $data, $user_id)
    {
    	$data['add_date'] = time();
    	$data['modify_date'] = time();

        return parent::insert($data);
    }

    public function update(array $data, $id, $user_id)
    {
    	$data['modify_date'] = time();
    	$data['is_ghost'] = 0;

		return parent::update($data, 'id = '.$id);
    }

    public function remove($items, $user_id)
    {
    	foreach($items as $k => $id)
    	{
	    	$data['modify_date'] = time();
	    	$data['is_removed'] = 1;

	    	parent::update($data, 'id = '.$id);
    	}

    	return true;
	}

	public function gorder($id, $gid, $d, $param = 'id_gallery')
    {
    	$sel = $this->getAdapter()->select()
    					->from($this->_name)
    					->where($param.' = ?', $gid)
    					->order('norder');

    	$data = $this->getList($sel)->toArray();

    	foreach($data as $k => $v)
    	{
    		if($v['id'] == $id)
    		{
    			if($d == 1){
    				parent::update(array('norder' => $k-1), 'id = '.$v['id']);
    				parent::update(array('norder' => $k), 'id = '.$data[$k-1]['id']);
    				break;
    			}

    			if($d == 2){
    				parent::update(array('norder' => $k+1), 'id = '.$v['id']);
    				parent::update(array('norder' => $k), 'id = '.$data[$k+1]['id']);
    				break;
    			}
    		}

    		parent::update(array('norder' => $k), 'id = '.$v['id']);
    	}
    }

    public function comment($data, $user_id = 0)
    {
    	$data['add_date'] = time();
    	$data['modify_date'] = time();
    	$data['add_user_id'] = $user_id;
    	$data['modify_user_id'] = $user_id;
    	$data['is_ghost'] = 0;

    	try{
    		$this->getAdapter()->insert('objects_comments', $data);
    	}catch (Exception $e)
    	{
    		echo $e->__toString();
    		die();
    	}

    	return $this->getAdapter()->lastInsertId('objects_comments');
    }

    public function commetns_count($id)
    {
    	$sel = $this->getAdapter()->select()
    					->from('objects_comments', array('count' => 'count(id)'))
    					->where('id_object = ?', $id);
    	$count = $this->getList($sel)->getRow(0)->toArray();

    	$d['comments_count'] = $count['count'];
    	parent::update($d, 'id = '.$id);
    }

    /********************
     ***  Decorators  ***
    *********************/

    public function simpleDecorator($key, $value)
    {
        switch ($key) {
            case 'modify_date':
            case 'add_date':
            	$result[$key]['__attributes'] = $this->timestampToArray($value);
            	break;
            default:
                $result[$key] = stripslashes($value);
                break;
        }
        return $result;
    }

    function make_seed() {
	    list($usec, $sec) = explode(' ', microtime());
	    return (float) $sec + ((float) $usec * 100000);
	}

    public function array_rand($a)
    {
		if(!is_array($a)) return;

    	srand($this->make_seed());
		$r = array();
		while(count($r) != count($a))
		{
			$s = rand(0, count($a)-1);
			if(!in_array($s, $r)) $r[] = $s;
		}

		$ret = array();
		foreach($r as $v)
			$ret[] = $a[$v];

		return $ret;
    }


}
?>